package com.xuerhua.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.dao.ParentInfoMapper;
import com.xuerhua.yxt.dao.ParentStudentRelMapper;
import com.xuerhua.yxt.model.ParentInfo;
import com.xuerhua.yxt.model.ParentStudentRel;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.ParentReqVO;
import com.xuerhua.yxt.service.IParentService;
import com.xuerhua.yxt.view.ParentPersonImportView;
import com.xuerhua.yxt.view.ParentRelStudentView;

@Service("parentService")
public class ParentServiceImpl implements IParentService {
	
	@Autowired
	private ParentInfoMapper parentInfoMapper;
	@Autowired
	private ParentStudentRelMapper parentStudentRelMapper;
	
	@Override
	public ParentInfo selectParentById(Integer id) {
		return parentInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public List<ParentInfo> selectListByIds(List<String> parentIds) {
		return parentInfoMapper.selectListByIds(parentIds);
	}

	@Override
	public ParentInfo selectByOpenid(String openid) {
		return parentInfoMapper.selectByOpenid(openid);
	}
	
	@Override
	public ParentStudentRel selectParentStudentRelById(Integer id) {
		return parentStudentRelMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int updateForParent(ParentInfo record) {
		return parentInfoMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int updateForParentRelStudent(ParentStudentRel record) {
		return parentStudentRelMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int selectCountByOpenid(String openid) {
		return parentInfoMapper.selectCountByOpenid(openid);
	}
	
	@Override
	public List<ParentRelStudentView> selectStudentListForParent(ParentReqVO vo) {
		return parentInfoMapper.selectStudentListForParent(vo);
	}
	
	@Override
	public int selectStudentCountForParent(ParentReqVO vo) {
		return parentInfoMapper.selectStudentCountForParent(vo);
	}
	
	@Override
	public int saveParentRelStudent(ParentRelStudentView view) {
		ParentReqVO reqVO = new ParentReqVO();
		reqVO.setStudentId(view.getId());
		reqVO.setOpenid(view.getOpenid());
		reqVO.setSchoolId(view.getSchoolId());
		int count = parentInfoMapper.selectStudentCountForParent(reqVO);
		if(count > 0) {
			return 1;
		}
		if(view.getId() == null) {
			parentInfoMapper.insertSelective(view);
		}
		ParentStudentRel rel = new ParentStudentRel();
		rel.setParentId(view.getId());
		rel.setStudentId(view.getStudentId());
		rel.setSchoolId(view.getSchoolId());
		return parentStudentRelMapper.insertSelective(rel);
	}
	
	@Override
	public List<ParentRelStudentView> selectListForClass(ParentReqVO vo) {
		return parentInfoMapper.selectListForClass(vo);
	}

	@Override
	public int updatePoints(Integer id, Float point) {
		int result = startUpdatePoints(id, point);
		if(result == 0) {
			for(int i = 0; i < 2; i++) {
				result = startUpdatePoints(id, point);
				if(result == 1) {
					break;
				}
			}
		}
		return result;
	}

	private int startUpdatePoints(Integer id, Float point) {
		ParentInfo parent = parentInfoMapper.selectByPrimaryKey(id);
		Float points = parent.getPoints() == null ? 0 : parent.getPoints();
		parent.setPoints(points + point);
		return parentInfoMapper.updateForVersionById(parent);
	}

	@Override
	public List<ParentRelStudentView> selectRelStudentByOpenid(String openid, Integer schoolId) {
		return parentInfoMapper.selectRelStudentByOpenid(openid,schoolId);
	}

	@Override
	public int delByRelId(ParentReqVO vo) {
		return parentStudentRelMapper.delByRelId(vo);
	}

	@Override
	public List<ParentInfo> selectList() {
		return parentInfoMapper.selectList();
	}

	@Override
	public List<ParentRelStudentView> selectParentListByStudentId(Integer studentId) {
		return parentInfoMapper.selectParentListByStudentId(studentId);
	}

	@Override
	public List<ParentPersonImportView> selectPersonImport(Page<ParentPersonImportView> page) {
		return parentInfoMapper.selectPersonImport(page);
	}

	@Override
	public ParentPersonImportView selectParentViewByStudent(Integer parentId, Integer studentId) {
		return parentInfoMapper.selectParentViewByStudent(parentId, studentId);
	}

	@Override
	public int selectNoPersonIdCount() {
		return parentInfoMapper.selectNoPersonIdCount();
	}

	@Override
	public int updateBatchPersonId(List<ParentInfo> list) {
		return parentInfoMapper.updateBatchPersonId(list);
	}

	@Override
	public List<ParentRelStudentView> selectParentListByStudyNo(String studyNo) {
		return parentInfoMapper.selectParentListByStudyNo(studyNo);
	}

}
